package com.rxnetwork.sample.mvp;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;

import com.rxnetwork.sample.R;
import com.rxnetwork.sample.amshook.AMSHookUtil;
import com.rxnetwork.sample.mvp.toolbar.BaseTopBarFragment;
import com.rxnetwork.sample.utils.network.AppNetWorkManager;

import java.lang.reflect.ParameterizedType;

import butterknife.ButterKnife;

public abstract class MVPBaseFragment<V extends BaseView, T extends BasePresenterImpl<V>> extends BaseTopBarFragment implements BaseView {
    public T mPresenter;

    public Dialog mViewLoading;

    private Dialog getmViewLoading() {
        if (mViewLoading == null) {
            mViewLoading = new Dialog(getActivity());
            View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.loading, null);
            mViewLoading.setContentView(dialogView);
            Window dialogWindow = mViewLoading.getWindow();
            dialogWindow.setGravity(Gravity.CENTER);
            dialogWindow.setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mViewLoading.setCanceledOnTouchOutside(false);
            mViewLoading.setCancelable(false);
        }
        return mViewLoading;
    }

    @Override
    protected void initPresenter() {
        mPresenter = getInstance(this, 1);
        mPresenter.attachView((V) this);
        ButterKnife.bind(getActivity());
//        AMSHookUtil.hookStartActivity(getActivity());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }
        //取消网络
        AppNetWorkManager.getInstance().clearSubscription();
    }

    @Override
    public Context getContext() {
        return super.getContext();
    }

    public <T> T getInstance(Object o, int i) {
        try {
            return ((Class<T>) ((ParameterizedType) (o.getClass()
                    .getGenericSuperclass())).getActualTypeArguments()[i])
                    .newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void baseShowLoading() {
        if (mPresenter != null && getmViewLoading() != null) {
            if (getmViewLoading().isShowing()) {
                getmViewLoading().dismiss();
            }
            getmViewLoading().show();
        }
    }

    @Override
    public void baseHideLoading() {
        if (mPresenter != null && getmViewLoading() != null) {
            if (getmViewLoading().isShowing()) {
                getmViewLoading().dismiss();
            }
        }
    }
}
